package com.igoal.entity;

import java.util.Date;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.SearchableProperty;

@Entity
@Table(name="user_role")
@AssociationOverrides({
	@AssociationOverride(name="pk.user", joinColumns=@JoinColumn(name="user_id")),
	@AssociationOverride(name="pk.role", joinColumns=@JoinColumn(name="role_id"))
})
public class UserRole extends BaseObject implements Audit {
	private static final long serialVersionUID = 7824097157995421801L;
	
	private UserRolePk pk = new UserRolePk();
	private Date createDate;
	private Date updateDate;
	private DataStatus status;	
	
	@Transient
	public User getUser(){
		return pk.getUser();
	}
	
	public void setUser(User user){
		pk.setUser(user);
	}
	
	@Transient
	public Role getRole(){
		return pk.getRole();
	}
	
	public void setRole(Role role){
		pk.setRole(role);
	}
	
	@EmbeddedId
	public UserRolePk getPk() {
		return pk;
	}

	public void setPk(UserRolePk pk) {
		this.pk = pk;
	}

	public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
 
        UserRole that = (UserRole) o;
 
        if (getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null) return false;
 
        return true;
    }
 
    public int hashCode() {
        return (getPk() != null ? getPk().hashCode() : 0);
    }
    
    @Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Override
	public String toString() {
		if(pk != null && pk.getUser() != null && pk.getRole() != null){
			return pk.getUser().toString() + " has role: " + pk.getRole().toString(); 
		}
		return null;
	}
}
